{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1e86fb8-c903-4f92-8ea8-9bfe95f8a4c3",
   "metadata": {},
   "source": [
    "### Generate CADD-SV input bed and config files for all INTERVAL SVs \n",
    "\n",
    "* Input to CADD SV cannot include \"INV\" class "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b16945f4-7ff2-4c95-9918-643665850ea7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import pandas as pd\n",
    "from pathlib import Path \n",
    "from pybedtools import BedTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "53de410c",
   "metadata": {},
   "outputs": [],
   "source": [
    "wkdir=\"/lustre/scratch126/humgen/projects/interval_rna/interval_rna_seq/thomasVDS/misexpression_v3\"\n",
    "wkdir_path = Path(wkdir)\n",
    "\n",
    "out_dir = wkdir_path.joinpath(\"5_misexp_vrnts/scores/cadd_sv/\")\n",
    "out_dir.mkdir(parents=True, exist_ok=True)\n",
    "# input directory \n",
    "input_dir = out_dir.joinpath(\"input\")\n",
    "input_dir.mkdir(parents=True, exist_ok=True)\n",
    "# config directory \n",
    "config_dir = out_dir.joinpath(\"config\")\n",
    "config_dir.mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2ed75e90-f3a8-4198-9dfb-3f4281dcb77b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of SVs: 123801\n"
     ]
    }
   ],
   "source": [
    "sv_info_path = \"/lustre/scratch126/humgen/projects/interval_rna/interval_rna_seq/thomasVDS/lof_missense/data/sv_vcf/info_table/final_sites_critical_info_allele.txt\"\n",
    "sv_info_df = pd.read_csv(sv_info_path, sep=\"\\t\", dtype={\"plinkID\": str}).rename(columns={\"plinkID\":\"vrnt_id\"})\n",
    "vrnt_ids = sv_info_df.vrnt_id.unique()\n",
    "print(f\"Number of SVs: {len(vrnt_ids)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "63094580-958a-4d34-abbd-7d5a1e1dc5b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of SVs no inversions: 121042\n"
     ]
    }
   ],
   "source": [
    "# remove inversions from input dataframe \n",
    "sv_info_no_inv_df = sv_info_df[sv_info_df[\"SVTYPE\"] != \"INV\"]\n",
    "vrnt_num_no_inv = len(sv_info_no_inv_df.vrnt_id.unique())\n",
    "print(f\"Number of SVs no inversions: {vrnt_num_no_inv}\")\n",
    "# write to file \n",
    "sv_info_no_inversions_path = out_dir.joinpath(\"final_sites_critical_info_allele.no_inversions.txt\")\n",
    "sv_info_no_inv_df.to_csv(sv_info_no_inversions_path, sep=\"\\t\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1b4e07b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "caddsv_path = \"/lustre/scratch126/humgen/projects/interval_rna/interval_rna_seq/thomasVDS/lof_missense/software/CADD-SV\"\n",
    "caddsv_dir = Path(caddsv_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d2dbd1b6-3246-40a0-916a-ccd40d065598",
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate SV bed files split by 5000 variants and associated config.yml file \n",
    "config_path_list = []\n",
    "bed_file_list = []\n",
    "lines_per_file = 5000\n",
    "bed_out = None\n",
    "with open(sv_info_no_inversions_path) as f_in:\n",
    "    for lineno, line in enumerate(f_in):\n",
    "        if lineno % lines_per_file == 0:\n",
    "            if bed_out:\n",
    "                bed_out.close()\n",
    "            bed_out_path = caddsv_dir.joinpath(f\"input/id_intrvl_svs_no_inv_{lineno+lines_per_file}.bed\")\n",
    "            bed_file_list.append(bed_out_path)\n",
    "            bed_out = open(bed_out_path, \"w\")\n",
    "            # generate input config\n",
    "            config_out_path = caddsv_dir.joinpath(f\"config/config_no_inv_{lineno+lines_per_file}.yml\")\n",
    "            config_path_list.append(config_out_path)\n",
    "            config_out = open(config_out_path, \"w\")\n",
    "            config_out.write(f\"---\\ndataset:\\n        - intrvl_svs_no_inv_{lineno+lines_per_file}\\n...\\n\")\n",
    "            config_out.close()\n",
    "        if line.startswith(\"plinkID\"):\n",
    "            continue\n",
    "        else: \n",
    "            chrom, start, end = line.split(\"\\t\")[2], line.split(\"\\t\")[3], line.split(\"\\t\")[4]\n",
    "            sv_type = line.split(\"\\t\")[5]\n",
    "            if sv_type == \"MEI\":\n",
    "                sv_type = \"INS\"\n",
    "            bed_out.write(f\"{chrom}\\t{start}\\t{end}\\t{sv_type}\\n\")\n",
    "    if bed_out:\n",
    "        bed_out.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "496f8fe6-9ec5-44c8-a3dd-9568bff867e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "config_path_out = out_dir.joinpath(\"config_paths_no_invs.txt\")\n",
    "with open(config_path_out, \"w\") as f: \n",
    "    for config_path in config_path_list: \n",
    "        f.write(str(config_path).split(\"/\")[-2] + \"/\" + str(config_path).split(\"/\")[-1] +\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "461bce45-d3dc-43b8-b6cc-76840875f48e",
   "metadata": {},
   "outputs": [],
   "source": [
    "bed_path_out = out_dir.joinpath(\"bed_paths_no_invs.txt\")\n",
    "with open(bed_path_out, \"w\") as f: \n",
    "    for bed_path in bed_file_list: \n",
    "        f.write(str(bed_path) +\"\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
